package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.hscrb.bean.BzPackaging;
import com.jy.adscm.operation.hscrb.mapper.mysql.BzPackagingMapper;
import com.jy.adscm.operation.hscrb.service.BzPackagingService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-17
 */
@Service
public class BzPackagingServiceImpl extends ServiceImpl<BzPackagingMapper, BzPackaging> implements BzPackagingService {
   @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateBzPackaging(BzPackaging bzPackaging, Token token) {
        //待办修改
        if (bzPackaging.getTaskstate()==0 && bzPackaging.getTaskfinishstate()==0 &&
                "0".equals(bzPackaging.getIstaskstart()) &&  "0".equals(bzPackaging.getIstaskbegin()) &&
                "0".equals(bzPackaging.getIstaskend())  && "0".equals(bzPackaging.getIsreviewtask())
        ){


            return  super.updateById(bzPackaging);
        }
        //接收任务
        if (bzPackaging.getTaskstate()==0 && bzPackaging.getTaskfinishstate()==0 &&
                "1".equals(bzPackaging.getIstaskstart()) &&  "0".equals(bzPackaging.getIstaskbegin()) &&
                "0".equals(bzPackaging.getIstaskend())  && "0".equals(bzPackaging.getIsreviewtask())
        ){
            bzPackaging.setIstaskstart("1");
            bzPackaging.setTaskstartaddtime(new Date());
            bzPackaging.setTaskstate(1);

            return  super.updateById(bzPackaging);
        }
        //开始任务
        if (bzPackaging.getTaskstate()==1 && bzPackaging.getTaskfinishstate()==0 &&
                "1".equals(bzPackaging.getIstaskstart()) &&  "1".equals(bzPackaging.getIstaskbegin()) &&
                "0".equals(bzPackaging.getIstaskend())  && "0".equals(bzPackaging.getIsreviewtask())
        ){
            bzPackaging.setIstaskbegin("1");
            bzPackaging.setTaskbeginaddtime(new Date());
            return super.updateById(bzPackaging);
        }
        //结束任务
        if (bzPackaging.getTaskstate()==1 && bzPackaging.getTaskfinishstate()==0 &&
                "1".equals(bzPackaging.getIstaskstart()) &&  "1".equals(bzPackaging.getIstaskbegin()) &&
                "1".equals(bzPackaging.getIstaskend())  && "0".equals(bzPackaging.getIsreviewtask())
        ){
            bzPackaging.setIstaskend("1");
            bzPackaging.setTaskfinishstate(1);
            bzPackaging.setTaskendaddtime(new Date());
            return super.updateById(bzPackaging);
        }
        //复核任务
        if (bzPackaging.getTaskstate()==1 && bzPackaging.getTaskfinishstate()==1 &&
                "1".equals(bzPackaging.getIstaskstart()) &&  "1".equals(bzPackaging.getIstaskbegin()) &&
                "1".equals(bzPackaging.getIstaskend())  && "1".equals(bzPackaging.getIsreviewtask())
        ){
            bzPackaging.setIsreviewtask("1");
            bzPackaging.setReviewaddtime(new Date());
            bzPackaging.setReviewperson(token.getUser().getId().toString());
            return super.updateById(bzPackaging);
        }

        return null;
    }

    @Override
    public List<BzPackaging> selectBzPackaging(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<BzPackaging> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<BzPackaging> bzPackagings= super.selectList(wrapper);



        for (BzPackaging bzPackaging : bzPackagings) {
            if ( bzPackaging.getTaskreleaseperson()!=null && !"".equals( bzPackaging.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(bzPackaging.getTaskreleaseperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    bzPackaging.setTaskreleasepersonName(jyUser.getName());
                }else {
                    bzPackaging.setTaskreleasepersonName(bzPackaging.getTaskreleaseperson());
                }

            }
            if ( bzPackaging.getTaskperformperson()!=null && !"".equals( bzPackaging.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(bzPackaging.getTaskperformperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    bzPackaging.setTaskperformpersonName(jyUser.getName());
                }else {
                    bzPackaging.setTaskperformpersonName(bzPackaging.getTaskperformperson());
                }

            }

            if ( bzPackaging.getReviewperson()!=null && !"".equals( bzPackaging.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(bzPackaging.getReviewperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    bzPackaging.setReviewpersonName(jyUser.getName());
                }else {
                    bzPackaging.setReviewpersonName(bzPackaging.getReviewperson());
                }

            }

        }

        return bzPackagings;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("109");
        return jyUsers;
    }
}
